package com.anti.modular.sys.emp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.emp.entity.SysEmpPos;
import com.anti.modular.sys.emp.service.SysEmpPosService;
import com.anti.modular.sys.pos.entity.SysPos;
import com.anti.modular.sys.pos.service.SysPosService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-11
 */
@Service
public class SysEmpPosServiceImpl implements SysEmpPosService {
    @Resource
    DBUtils dbUtils;

    @Resource
    private SysPosService sysPosService;
    /**
     * 职位id参数键
     */
    private static final String POS_ID_DICT_KEY = "posId";

    /**
     * 职位编码参数键
     */
    private static final String POS_CODE_DICT_KEY = "posCode";

    /**
     * 职位名称参数键
     */
    private static final String POS_NAME_DICT_KEY = "posName";
    @Override
    public boolean hasPosEmp(Long posId) {
        String sql="select count(*) from sys_emp_pos where pos_id=?";
        Integer integer = dbUtils.getFirst(sql, Integer.class, posId);
        return integer != 0;
    }

    @Override
    public void addOrEdit(Long empId, List<Long> posIdList) {
        //删除职位信息
        String removeSql="delete from sys_emp_pos where emp_id =? ";
        dbUtils.execSql(removeSql,empId);
        //保存职位信息
        String insertSql="insert into sys_emp_pos (emp_id,pos_id) values (?,?)";
        posIdList.forEach(posId -> {
            dbUtils.execSql(insertSql,empId,Convert.toLong(posId));
        });
    }

    @Override
    public List<Dict> getEmpPosDictList(Long empId, boolean isFillId) {
        List<Dict> dictList = CollectionUtil.newArrayList();
        String sql="select * from sys_emp_pos where emp_id=?";
        List<SysEmpPos> empPosList = dbUtils.query(sql, SysEmpPos.class, empId);
        empPosList.forEach(sysEmpPos -> {
            Dict dict = Dict.create();
            Long posId = sysEmpPos.getPosId();
            SysPos sysPos = sysPosService.getById(posId);
            if (isFillId) {
                dict.put(POS_ID_DICT_KEY, posId);
            }
            dict.put(POS_CODE_DICT_KEY, sysPos.getCode());
            dict.put(POS_NAME_DICT_KEY, sysPos.getName());
            dictList.add(dict);
        });
        return dictList;
    }

    @Override
    public void deleteEmpPosInfoByUserId(Long empId) {
        String sql="delete from sys_emp_pos where emp_id=?";
        dbUtils.execSql(sql,empId);
    }
}
